package com.fxd.shopping.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.fxd.model.product.ProductAttrValueEntity;
import com.fxd.shopping.product.dto.AttrInfoDTO;
import com.fxd.shopping.product.param.AttrSaveParam;
import com.fxd.shopping.product.service.ProductAttrValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.fxd.shopping.product.service.AttrService;
import com.fxd.core.util.PageUtils;
import com.fxd.core.util.RR;


/**
 * 商品属性
 *
 * @author fuxdong
 * @email 2016335176@qqcom
 * @date 2021-07-17 10:30:51
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public RR list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return RR.ok().put("page", page);
    }

    @GetMapping("/base/listforspu/{spuId}")
    public RR baseAttrList(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> list = productAttrValueService.baseAttrListForSpu(spuId);

        return RR.ok().put("data", list);
    }

    @RequestMapping("/{attrType}/list/{catelogId}")
    public RR baseAttrList(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId, @PathVariable("attrType") String attrType) {

        PageUtils page = attrService.queryBaseAttrPage(params, catelogId, attrType);

        return RR.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public RR info(@PathVariable("attrId") Long attrId) {
        AttrInfoDTO attr = attrService.getAttrInfo(attrId);

        return RR.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public RR save(@RequestBody AttrSaveParam attr) {
        attrService.saveAttr(attr);

        return RR.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public RR update(@RequestBody AttrSaveParam attr) {
        attrService.updateAttr(attr);

        return RR.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update/{spuId}")
    public RR updateSpuAttr(@PathVariable("spuId") Long spuId, @RequestBody List<ProductAttrValueEntity> entities) {
        productAttrValueService.updateSpuAttr(spuId, entities);

        return RR.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public RR delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return RR.ok();
    }

}
